using CoreManager.Models.Entities;
using SqlSugar;
using System.Linq.Expressions;

namespace CoreManager.Repository.Repositories
{
    /// <summary>
    /// SqlSugar仓储接口
    /// 继承自通用仓储接口，提供SqlSugar特有的功能
    /// </summary>
    /// <typeparam name="T">实体类型，必须继承自BaseEntity</typeparam>
    public interface ISugarRepository<T> : IRepository<T> where T : BaseEntity, new()
    {
        /// <summary>
        /// 获取SqlSugar数据库客户端
        /// </summary>
        ISqlSugarClient SugarClient { get; }

        /// <summary>
        /// 获取当前实体的可查询对象
        /// </summary>
        /// <returns>ISugarQueryable查询对象</returns>
        ISugarQueryable<T> AsQueryable();

        /// <summary>
        /// 执行原生SQL查询
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>查询结果列表</returns>
        Task<List<T>> SqlQueryAsync(string sql, object? parameters = null);

        /// <summary>
        /// 执行原生SQL命令
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>受影响的行数</returns>
        Task<int> ExecuteSqlAsync(string sql, object? parameters = null);

        /// <summary>
        /// 批量插入（高性能）
        /// </summary>
        /// <param name="entities">实体列表</param>
        /// <returns>插入的行数</returns>
        Task<int> BulkInsertAsync(List<T> entities);

        /// <summary>
        /// 批量更新（高性能）
        /// </summary>
        /// <param name="entities">实体列表</param>
        /// <returns>更新的行数</returns>
        Task<int> BulkUpdateAsync(List<T> entities);

        /// <summary>
        /// 批量删除（高性能）
        /// </summary>
        /// <param name="expression">删除条件</param>
        /// <returns>删除的行数</returns>
        Task<int> BulkDeleteAsync(Expression<Func<T, bool>> expression);

        /// <summary>
        /// 开始事务
        /// </summary>
        /// <returns>事务对象</returns>
        ITenant BeginTransaction();

        /// <summary>
        /// 复杂查询构建器
        /// </summary>
        /// <returns>查询构建器</returns>
        ISugarQueryable<T> Queryable();
    }
}